﻿CREATE PROCEDURE [acms].[TopicFile_Put]
	@TopicId int, 
	@FileId int, 
	@Name varchar(50), 
	@VersionID int,
	@IdOut int output,
	@SizeOut bigint output, 
	@FileTypeOut int output, 
	@ContentTypeOut varchar(50) output, 
	@ExtensionOut varchar(6) output, 
	@WidthOut int output, 
	@HeightOut int output,
	@VersionIdOut timestamp output
AS
	if exists(Select null from TopicFile_View where TopicId = @TopicId and FileId = @FileId) BEGIN
		Update TopicFiles
		Set
			Name = @Name
		Where TopicId = @TopicId and FileId = @FileID and VersionId = @VersionId
		if @@Rowcount = 0
			return -100
	END
	ELSE BEGIN
		insert into TopicFiles
			(TopicId, FileId, Name)
		values
			(@TopicId, @FileId, @Name)
	END
	select 
		@IdOut = Id,
		@SizeOut = Size, 
		@FileTypeOut = FileType, 
		@ContentTypeOut = ContentType, 
		@ExtensionOut = Extension, 
		@WidthOut = Width, 
		@HeightOut = Height,
		@VersionIdOut = VersionId
	From
		TopicFile_View
	where 
		TopicId = @TopicId and FileId = @FileId
RETURN 0;